package com.xinnet.mms.customerstate.service.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xinnet.core.utils.page.PageView;
import com.xinnet.mms.customerstate.dao.CustomerStateHistDao;
import com.xinnet.mms.customerstate.service.CustomerStateHistService;
import com.xinnet.mms.entity.CustomStateHist;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.entity.Dept;
import com.xinnet.mms.entity.Emp;
import com.xinnet.mms.entity.Notice;
import com.xinnet.mms.entity.Product;
import com.xinnet.mms.entity.PushHistory;
import com.xinnet.mms.utils.CustomerManage;
/**
 * 
 * @author 王湛智
 *
 */
@Service("customerStateHistService")
public class CustomerStateHistServiceImpl implements CustomerStateHistService {

	@Autowired
	private CustomerStateHistDao customerStateHistDao;
	/**
	 * 插入一条人员客户关系
	 */
	@Override
	public void addCustomerStateHist(CustomStateHist customerStateHist) throws SQLException {
		customerStateHistDao.addCustomerStateHist(customerStateHist);
	}
	
	/**
	 * 封装客户人员关系历史表数据
	 */
	@Override
	public CustomStateHist packageCustomStateHist(Emp emp, Customer customer, Dept dept, Product product, PushHistory pushHistory) throws SQLException {
		CustomStateHist customStateHist=new CustomStateHist();
		customStateHist.setId(UUID.randomUUID().toString());
		customStateHist.setProductId(product.getId());
		customStateHist.setCustomId(customer.getCusId());
		customStateHist.setCustomType(customer.getCustomerType());
		customStateHist.setCustomState(CustomerManage.ACCEPT_NO);
		customStateHist.setEmpId(emp.getEmpno());
		customStateHist.setPusherId(customer.getPusherId());
		customStateHist.setIsReceive(CustomerManage.ACCEPT_NO);
		customStateHist.setPushHistId(pushHistory.getId());
		customStateHist.setPushDesc(customer.getRemark());
		customStateHist.setProductCode(product.getProductCode());
		return customStateHist;
	}
	
	/**
	 * 根据customId 获取CustomStateHist
	 */
	public List<CustomStateHist> getReceiveCustomStateHistByCustomId(String customId){
		
		List<CustomStateHist> customStateHistList = customerStateHistDao.getReceiveCustomStateHistByCustomId(customId);
		
		return customStateHistList;
		
	}
	/**
	 * 根据customId productId获取CustomStateHist
	 */
	public List<CustomStateHist> getReceiveCustomStateHistByCustomIdAndProductId(String customId,String productId){
		
		List<CustomStateHist> customStateHistList = customerStateHistDao.getReceiveCustomStateHistByCustomIdAndProductId(customId,productId);
		
		return customStateHistList;
	}
	/**
	 * 更新状态历史表
	 */
	public void updateCustomStateHistByCustomIdAndEmpNo(String customId,String empNo,String productId){
		
		customerStateHistDao.updateCustomStateHistByCustomIdAndEmpNo(customId,empNo,productId);
	}

	/**
	 * 
	 * 功能描述：修改客户关系历史表接收状态
	 * 
	 * @author 王湛智
	 * 
	 * @param cusId
	 * @param empno
	 * @param state
	 */
	@Override
	public void updateCustomStateByCustomIdAndEmpno(String cusId,String empno, Integer customerState) {
		customerStateHistDao.updateCustomStateByCustomIdAndEmpno(cusId, empno, customerState);
	}

	/**
	 * 根据客户ID 销售ID删除客户关系历史
	 */
	@Override
	public void delCustomStateHistByCusIdAndEmpno(String cusId, String empno, String productCode) throws SQLException {
		customerStateHistDao.delCustomStateHistByCusIdAndEmpno(cusId, empno, productCode);
	}
	
	/**
	 * 根据业绩保留期 判断客户是否可以进入公海
	 * @param scoreHoldTime
	 * @return
	 */
	public boolean isCanGoOpenSeaByScoreHoldTime(String customId,String scoreHoldTime){
		boolean isCanGoOpenSea = customerStateHistDao.isCanGoOpenSeaByScoreHoldTime(customId, scoreHoldTime);
		return isCanGoOpenSea;
	}
	
	
	/**
	 * 根据业绩保留期  产品ID  客户ID    获取CustomStateHist
	 * @param customId
	 * @param scoreHoldTime
	 * @param productId
	 * @return
	 */
	public List<CustomStateHist> getCustomStateHistByScoreHoldTimeAndCustomIdAndProductId(String customId,String scoreHoldTime,String productId){
		
		List<CustomStateHist> customStateHistList = customerStateHistDao.getCustomStateHistByScoreHoldTimeAndCustomIdAndProductId( customId, scoreHoldTime, productId);
		return customStateHistList;
	}

	@Override
	public PageView<CustomStateHist> getCustomStateHistByCustomId(PageView<CustomStateHist> pageView, String customId) {
		pageView.setRecords(customerStateHistDao.getCustomStateHistByCustomId(pageView, customId));
		return pageView;
	}

	/**
	 * 用pushHistId修改状态历史表
	 */
	@Override
	public void updateCustomStateHistByPushHistId(String pushHistId) throws Exception {
		customerStateHistDao.updateCustomStateHistByPushHistId(pushHistId);
	}

	/**
	 * 根据ID修改状态历史表
	 */
	@Override
	public void updateCustomStateHistByCshId(CustomStateHist customStateHist) throws SQLException {
		customerStateHistDao.updateCustomStateHistByCshId(customStateHist);
	}
	/**
	 * 根据时间区间查询关系表记录
	 */
	@Override
	public List<CustomStateHist> getCshByTime(String timeUp, String timeDown) {
		Notice notice=new Notice();
		notice.setTimeUp(timeUp);
		notice.setTimeDown(timeDown);
		return customerStateHistDao.getCshByTime(notice);
	}

	@Override
	public List<CustomStateHist> getCshByTimeAndEmpno(Emp emp) {
		
		return customerStateHistDao.getCshByTimeAndEmpno(emp);
	}

	/**
	 * 根据时间区间和客户类型查询关系表记录
	 */
	@Override
	public List<CustomStateHist> getCshByTimeAndCustomerType(String timeUp, String timeDown, Integer customerType) {
		Customer customer = new Customer();
		customer.setTimeAmount(timeUp+" 00:00:00");
		customer.setTimeStamp(timeDown+" 23:59:59");
		customer.setCustomerType(customerType);
		return customerStateHistDao.getCshByTimeAndCustomerType(customer);
	}
	
	public CustomStateHist getCustomStateHistByCondition(CustomStateHist customerStateHist){
		return customerStateHistDao.getCustomStateHistByCondition(customerStateHist);
	}
}
